<template>
    <div>
        <form id="addFriend" @submit="postEvent()" action="#">
            <input type="text" placeholder="请输入券友号id" class="userIpt2" v-model="searchId" @keyup="examine">
        </form>
        <iframe id="rfFrame" name="rfFrame" style="display:none;"></iframe>
        <div @click="postEvent()" class="addFriendBtn">添加</div>
    </div>
</template>
<script>
    import * as types from '../../vuex/types.js'
    import { mapState } from 'vuex'
    import getData from '../../service/getData.js'
    import { Toast } from 'mint-ui'
    export default {
        data:function(){
            return{
                searchId:''
            }
        },
        created:function(){
        },
        computed:{
            ...mapState({
                userInfo:state => state.user.userInfo
            })
        },
        methods:{
            friendDetail:function(){
                this.$router.push({'path':'/friendDetail'})
            },
            postEvent:function(){
                let me = this;
                document.forms[0].target="rfFrame";
                if(!/^\d+$/.test(this.searchId)) {
                    Toast({
                        message: '只能输入数字',
                        position: 'top',
                        duration: 1000
                    });
                    return false;
                }
                if(me.searchId==me.userInfo.usrId){
                    Toast({
                        message: '不能搜索自己',
                        position: 'top',
                        duration: 1000
                    });
                    return false;
                }
                getData.searchFriend({
                    method:'post',data:{  "friendId": this.searchId, "usrId":this.userInfo.usrId}//
                }).then(function(rep){
                    if(rep.respCode == '00'){
                        console.log(rep)
                        if(rep.content){
                            // console.info(rep);
                            me.$store.commit("search_friend",rep.content);
                            me.$router.push({"path":'/addFriend'})
                        }else{
                            Toast({
                                message: '用户不存在',
                                position: 'top',
                                duration: 800
                            });
                        }
                    }else{
                       Toast({
                            message: '操作失败',
                            position: 'top',
                            duration: 800
                        });
                    }

                }).catch(function(rep){

                });
                return false;

            },
            examine:function(){
                let me = this;
                if(!/^\d+$/.test(this.searchId)) {
                    Toast({
                        message: '只能输入数字',
                        position: 'top',
                        duration: 1000
                    });
                }
            }
        }
    }
</script>
<style>
</style>
